1 using UnityEngine;
2 using
System.Collections;
3
4 namespace
GamePlay
5 {
6     
public class Animal : MonoBehaviour
7     {
8         
public int animalIndex;
9
10         
public GameScreen gameScreen;
11
12         
[SerializeField]
13         
private int state;
14
15         
private float speedX;
16         
private float speedY;
17
18         
private int stepJump;
19
20         
private bool isBooster;
21         
private float boosterTime;
22
23         
private bool isSprings;
24
25         
private Sprite shadowSprite;
26         
private float shadowCreateTime;
27
28         
private Vector3 revivalPosition;
29
30         
private bool isRevival;
31         
private float revivalTime;
32
33         
public bool isStanding;
34         
private float standTime;
35
36         
public bool IsProtected { get; set; }
37         
public float ProtectedTime { get; set; }
38
39         
public bool IsSpeedUp { get; set; }
40         
public float SpeedUpTime { get; set; }
41         
private bool IsSpeedUpStart;
42
43         
[SerializeField]
44         
private bool isRunning;
45
46         
public Effect protectedEffect { get; set; }
47
48         
private AnimalProperties properties;
49
50         
[SerializeField]
51         
private Rigidbody2D animalBody;
52         
void Start()
53         {
54             CircleCollider2D animalCollider = gameObject.AddComponent<CircleCollider2D>();
55             animalCollider.radius =
0.3f;
56             animalCollider.offset =
new Vector2(0, 0.27f);
57             animalCollider.sharedMaterial = Resources.Load<PhysicsMaterial2D>(
"AnimalPhysics");
58             animalBody = gameObject.AddComponent<Rigidbody2D>();
59             
//animalBody.fixedAngle = true;
60             animalBody.constraints = RigidbodyConstraints2D.FreezeRotation;
61             state = JUMPING;
62             
//gameObject.name = "Animal";
63
64             
//test
65             
//if (animalIndex != 0) gameObject.SetActive(false);
66
67             stepJump =
2;
68             isRunning =
true;
69         }
70
71         
public void setAnimalProperties(float speedX_decimal, float speedY_decimal)
72         {
73             properties =
new AnimalProperties(speedX_decimal, speedY_decimal);
74             
// properties.printInfo();
75         }
76
77         
public void setAnimalName(string animalName)
78         {
79             shadowSprite = Resources.Load<Sprite>(
"Animals/Shadows/" + animalName);
80         }
81
82         
private void createShadow()
83         {
84             
if (animalIndex != 0) return;
85             shadowCreateTime += Time.deltaTime;
86             
if (shadowCreateTime >= 0.05f)
87             {
88                 shadowCreateTime =
0;
89                 GameObject shadowObject =
new GameObject("Shadow");
90                 shadowObject.transform.parent = gameScreen.shadowLayer.transform;
91                 shadowObject.layer = LayerMask.NameToLayer(
"AnimalStand");
92                 shadowObject.transform.localPosition = transform.localPosition +
new Vector3(0, 0.3f, 0);
93                 shadowObject.AddComponent<SpriteRenderer>().sprite = shadowSprite;
94                 shadowObject.GetComponent<SpriteRenderer>().sortingLayerName =
"MapObject";
95                 shadowObject.GetComponent<SpriteRenderer>().color =
new Color(1, 1, 1, 0.8f);
96                 shadowObject.AddComponent<Actor>().addAction(
new ActionSequence(
97                     
new ActionColorTo(0, 0, 0, 0, 0.5f),
98                     
new ActionRunnable(delegate ()
99                 {
100                     Destroy(shadowObject);
101                 })
102                 ));
103             }
104         }
105
106         
public void Jump()
107         {
108             
if (stepJump > 0 && !isStanding && !isRevival)
109             {
110                 isSprings =
false;
111                 state = JUMP;
112                 stepJump--;
113                 
if (animalIndex == 0)
114                     SoundManager.playSound(
"Sounds/jump");
115             }
116         }
117
118         
private void booster()
119         {
120             isBooster =
true;
121             boosterTime =
2;
122             isSprings =
false;
123             
if (animalIndex == 0)
124                 SoundManager.playSound(
"Sounds/booster");
125         }
126
127         
private void springs()
128         {
129             isSprings =
true;
130             isBooster =
false;
131             state = JUMP;
132             
if (animalIndex == 0)
133                 SoundManager.playSound(
"Sounds/springs");
134         }
135
136         
private void SetVelocity(int action)
137         {
138             
switch (action)
139             {
140                 
case RUN:
141                     
if (isBooster)
142                     {
143                         
//speedX = 7;
144                         speedX = properties.speedXBooster;
145                         speedY =
0;
146                     }
147                     
else
148                     {
149                         
//speedX = 3;
150                         speedX = properties.speedX;
151                         speedY =
0;
152                     }
153                     
break;
154                 
case JUMP:
155                     
if (isBooster)
156                     {
157                         
//speedX = 7;
158                         speedX = properties.speedXBooster;
159                         
//speedY = 5;
160                         speedY = properties.speedYBooster;
161                     }
162                     
else if (isSprings)
163                     {
164                         
//speedX = 9;
165                         speedX = properties.speedXSprings;
166                         
//speedY = 7;
167                         speedY = properties.speedYSprings;
168                     }
169                     
else
170                     {
171                         
//speedX = 3;
172                         speedX = properties.speedX;
173                         
//speedY = 5;
174                         speedY = properties.speedY;
175                     }
176                     
break;
177                 
case JUMPING:
178                     
break;
179             }
180         }
181
182         
public void changeToState(string state_key)
183         {
184             Animator animator = gameObject.GetComponent<Animator>();
185             
switch (state_key)
186             {
187                 
case STATE_RUN:
188                     animator.Play(
"run" + (animalIndex == 0 ? "" : "_blue"));
189                     
/*
190                     animator.SetBool(STATE_SHOCK,
false);
191                     animator.SetBool(STATE_FIRE,
false);
192                     animator.SetBool(STATE_RUN,
true);
193                     animator.SetBool(IS_PLAYER, animalIndex ==
0);
194                      * */

195                     
break;
196                 
case STATE_SHOCK:
197                     animator.Play(
"run_choang" + (animalIndex == 0 ? "" : "_blue"));
198                     
/*
199                     animator.SetBool(STATE_SHOCK,
true);
200                     animator.SetBool(STATE_FIRE,
false);
201                     animator.SetBool(STATE_RUN,
false);
202                      * */

203                     
break;
204                 
case STATE_FIRE:
205                     animator.Play(
"run_black");
206                     
/*
207                     animator.SetBool(STATE_SHOCK,
false);
208                     animator.SetBool(STATE_FIRE,
true);
209                     animator.SetBool(STATE_RUN,
false);
210                      * */

211                     
break;
212             }
213         }
214
215         
public void Update()
216         {
217             
if (isRunning)
218             {
219                 
if (isStanding)
220                 {
221                     standTime -= Time.deltaTime;
222                     
if (standTime <= 0)
223                     {
224                         isStanding =
false;
225                         setStand(
false, 0, STATE_RUN);
226                     }
227                 }
228                 
else if (IsSpeedUp)
229                 {
230                     SpeedUpTime -= Time.deltaTime;
231                     
if (!IsSpeedUpStart)
232                     {
233                         
if (transform.localPosition.y <= 1f)
234                             animalBody.velocity =
new Vector2(4, 5);
235                         
else
236                             IsSpeedUpStart =
true;
237                     }
238                     
else
239                         animalBody.velocity =
new Vector2(12, 0);
240
241                     
if (SpeedUpTime <= 0)
242                     {
243                         IsSpeedUp =
false;
244                     }
245                 }
246                 
else
247                 {
248                     UpdateAnimal();
249                 }
250
251                 
if (IsProtected)
252                 {
253                     ProtectedTime -= Time.deltaTime;
254                     
if (ProtectedTime <= 0)
255                         IsProtected =
false;
256                 }
257
258             }
259         }
260
261         
private void UpdateAnimal()
262         {
263             SetVelocity(state);
264             
switch (state)
265             {
266                 
case RUN:
267                 
//GetComponent<Rigidbody2D>().velocity = new Vector2(speedX, speedY);
268                 
//break;
269                 
case JUMP:
270                     animalBody.velocity =
new Vector2(speedX, speedY);
271                     state = JUMPING;
272                     
break;
273                 
case JUMPING:
274                     
break;
275             }
276             
if (isBooster)
277             {
278                 boosterTime -= Time.deltaTime;
279                 
if (boosterTime <= 0)
280                 {
281                     isBooster =
false;
282                 }
283                 createShadow();
284             }
285             
else if (isSprings)
286             {
287                 createShadow();
288             }
289
290             
if (isRevival)
291             {
292                 revivalTime -= Time.deltaTime;
293                 
if (revivalTime <= 0)
294                 {
295                     revival();
296                 }
297             }
298         }
299
300         
public void OnCollisionEnter2D(Collision2D otherCollision)
301         {
302             
if (otherCollision.gameObject.name == MapObjectNames.GroundObject)
303             {
304                 state = RUN;
305                 stepJump =
2;
306                 isSprings =
false;
307             }
308             
else if (otherCollision.gameObject.name == "Bullet")
309             {
310                 
if (gameObject.GetComponent<Animal>().IsProtected || gameObject.GetComponent<Animal>().isStanding) return;
311                 
if (animalIndex != otherCollision.gameObject.GetComponent<Bullet>().animalIndex)
312                 {
313                     
if (otherCollision.gameObject.GetComponent<Bullet>().skillType == SkillType.THIENTHACH || otherCollision.gameObject.GetComponent<Bullet>().skillType == SkillType.BOM)
314                     {
315                         
if (otherCollision.gameObject.GetComponent<Bullet>().animalIndex == 0)
316                         {
317                             gameScreen.addScore(
10, gameObject.transform.localPosition);
318                             gameScreen.setCombo(
1, otherCollision.gameObject.GetComponent<Bullet>().skillType);
319                         }
320                         gameScreen.AnimalcollisionWithBullet(gameObject, otherCollision.gameObject);
321                     }
322                 }
323             }
324         }
325
326         
public void OnTriggerEnter2D(Collider2D otherCollider)
327         {
328             
string mapObjectName = otherCollider.gameObject.name;
329
330             
if (mapObjectName == MapObjectNames.MarkObject && animalIndex != 0)
331             {
332                 
int r = Random.Range(0, 2);
333                 
if (r == 0)
334                     
if (!isBooster && !isSprings)
335                         Jump();
336             }
337             
else if (mapObjectName == MapObjectNames.HoleObject && animalIndex != 0)
338             {
339                 Jump();
340             }
341             
else if (mapObjectName == MapObjectNames.BoostObject)
342             {
343                 booster();
344                 stepJump =
2;
345                 
if (animalIndex == 0)
346                 {
347                     gameScreen.setCombo(
6);
348                     gameScreen.addScore(
5, transform.localPosition);
349                 }
350             }
351             
else if (mapObjectName == MapObjectNames.SpringsObject)
352             {
353                 springs();
354                 stepJump =
2;
355                 
if (animalIndex == 0)
356                 {
357                     gameScreen.setCombo(
7);
358                     gameScreen.addScore(
5, transform.localPosition);
359                 }
360             }
361             
else if (mapObjectName == MapObjectNames.RevivalObject)
362             {
363                 setRevivalPosition(otherCollider.gameObject.GetComponent<RevivalObject>().revialIndex +
1);
364             }
365             
else if (mapObjectName == MapObjectNames.DeadLine)
366             {
367                 
if (animalIndex == 0)
368                 {
369                     gameScreen.showRevivalTask(
2, EmotionType.BUON);
370                 }
371                 isRevival =
true;
372                 setStand(
false, 0, STATE_RUN);
373                 IsProtected =
false;
374                 revivalTime =
2;
375             }
376             
else if (mapObjectName == MapObjectNames.SkillObject)
377             {
378                 gameScreen.eatSkillRandom(otherCollider.gameObject.GetComponent<SkillRandom>().skillType,
379                        otherCollider.gameObject.transform.localPosition, animalIndex);
380                 
if (animalIndex == 0)
381                     SoundManager.playSound(
"Sounds/anskill");
382
383             }
384             
else if (mapObjectName == MapObjectNames.FinishObject && animalIndex == 0)
385             {
386                 gameScreen.finishGame();
387
388                 Destroy(otherCollider);
//cho nay de tranh lap lai nhieu lan ham finishgame
389             }
390             
else if (mapObjectName == MapObjectNames.Bullet)
391             {
392                 
if (animalIndex != otherCollider.gameObject.GetComponent<Bullet>().animalIndex)
393                 {
394                     
if (IsProtected) return;
395                     gameScreen.AnimalcollisionWithBullet(gameObject, otherCollider.gameObject);
396                     isSprings =
false;
397                     isBooster =
false;
398
399                     
if (otherCollider.gameObject.GetComponent<Bullet>().animalIndex == 0)
400                     {
401                         gameScreen.addScore(
10, gameObject.transform.localPosition);
402                         gameScreen.setCombo(
1, otherCollider.gameObject.GetComponent<Bullet>().skillType);
403                     }
404                     
else
405                     {
406                         gameScreen.setCombo(-
1);
407                     }
408                 }
409             }
410             
else if (otherCollider.gameObject.name == MapObjectNames.Coin && animalIndex == 0)//if is player
411             {
412                 gameScreen.addDurtGold(otherCollider.gameObject.transform.localPosition);
413                 Destroy(otherCollider.gameObject);
414                 gameScreen.m_gold++;
415                 SoundManager.playSound(
"Sounds/anxu");
416             }
417         }
418
419         
public void OnCollisionExit2D(Collision2D otherCollision)
420         {
421             
if (otherCollision.gameObject.name == MapObjectNames.GroundObject)
422             {
423                 
if (state == RUN)
424                 {
425                     state = JUMPING;
426                 }
427             }
428         }
429
430         
private void setRevivalPosition(int revivalIndex)
431         {
432             revivalPosition = gameScreen.getRevivalPosition(revivalIndex);
433         }
434
435         
public void revival()
436         {
437             transform.localPosition =
new Vector3(revivalPosition.x / 100 - 4, revivalPosition.y / 100 + 2.5f, transform.localPosition.z);
438             isRevival =
false;
439         }
440
441         
public bool IsRevival()
442         {
443             
return this.isRevival;
444         }
445
446         
public void setRunning(bool isRunning)
447         {
448             
this.isRunning = isRunning;
449             gameObject.GetComponent<Animator>().enabled = isRunning;
450             gameObject.GetComponent<Rigidbody2D>().simulated = isRunning;
451         }
452
453         
public void setStand(bool isStand, float standTime, string state_key)
454         {
455             
this.isStanding = isStand;
456             
this.standTime = standTime;
457             changeToState(state_key);
458             
if (isStand)
459             {
460                 gameObject.layer = LayerMask.NameToLayer(
"AnimalStand");
461                 
//gameObject.GetComponent<Animator>().Play("run_black");
462             }
463             
else
464             {
465                 gameObject.layer = LayerMask.NameToLayer(
"Animal" + (animalIndex + 1));
466                 
//gameObject.GetComponent<Animator>().Play("run" + (animalIndex == 0?"":"_blue"));
467             }
468         }
469
470         
public void setProtected()
471         {
472             IsProtected =
true;
473             ProtectedTime =
5;
474         }
475
476         
public void setSpeedUp()
477         {
478             IsSpeedUp =
true;
479             SpeedUpTime =
5;
480             IsSpeedUpStart =
false;
481         }
482
483         
private const int JUMP = 1;
484         
private const int JUMPING = 2;
485         
private const int RUN = 3;
486
487         
public const string STATE_SHOCK = "shock";
488         
public const string STATE_RUN = "run";
489         
public const string STATE_FIRE = "fire";
490         
public const string IS_PLAYER = "isplayer";
491     }
492 }


animalBody.fixedAngle = true;

gameObject.name = "Animal";

test

if (animalIndex != 0) gameObject.SetActive(false);

properties.printInfo();

speedX = 7;

speedX = 3;

speedX = 7;

speedY = 5;

speedX = 9;

speedY = 7;

speedX = 3;

speedY = 5;

GetComponent().velocity = new Vector2(speedX, speedY);

break;

Destroy(otherCollider);cho nay de tranh lap lai nhieu lan ham finishgame

else if (otherCollider.gameObject.name == MapObjectNames.Coin && animalIndex == 0)if is player

gameObject.GetComponent().Play("run_black");

gameObject.GetComponent().Play("run" + (animalIndex == 0?"":"_blue"));




Trò chơi đua xe động vật trong UNITY Engine 114.727 lượt xem

Gõ tìm kiếm nhanh...